package com.guiji.template.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.guiji.common.utils.PageUtils;
import com.guiji.common.core.page.PagePlus;
import com.guiji.common.core.page.TableDataInfo;
import com.guiji.template.domain.TemplateField;
import com.guiji.template.mapper.TemplateFieldMapper;
import com.guiji.template.service.ITemplateFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.guiji.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guiji.template.bo.TemplateAddBo;
import com.guiji.template.bo.TemplateQueryBo;
import com.guiji.template.bo.TemplateEditBo;
import com.guiji.template.domain.Template;
import com.guiji.template.mapper.TemplateMapper;
import com.guiji.template.vo.TemplateVo;
import com.guiji.template.service.ITemplateService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 模板Service业务层处理
 *
 * @author 钟维澎（四月）
 * @date 2021-07-01
 */
@Service
public class TemplateServiceImpl extends ServicePlusImpl<TemplateMapper, Template> implements ITemplateService {

	@Autowired
	private ITemplateFieldService iTemplateFieldService;

	@Override
	public TemplateVo queryById(Long id){
		return getVoById(id, TemplateVo.class);
	}

	@Override
	public TableDataInfo<TemplateVo> queryPageList(TemplateQueryBo bo) {
		PagePlus<Template, TemplateVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), TemplateVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<TemplateVo> queryList(TemplateQueryBo bo) {
		return listVo(buildQueryWrapper(bo), TemplateVo.class);
	}

	private LambdaQueryWrapper<Template> buildQueryWrapper(TemplateQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<Template> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getId() != null, Template::getId, bo.getId());
		lqw.eq(bo.getType() != null, Template::getType, bo.getType());
		lqw.like(StrUtil.isNotBlank(bo.getName()), Template::getName, bo.getName());
		lqw.eq(bo.getVersion() != null, Template::getVersion, bo.getVersion());
		lqw.eq(bo.getCreateTime() != null, Template::getCreateTime, bo.getCreateTime());
		lqw.eq(StrUtil.isNotBlank(bo.getCreateBy()), Template::getCreateBy, bo.getCreateBy());
		lqw.eq(bo.getUpdateTime() != null, Template::getUpdateTime, bo.getUpdateTime());
		lqw.eq(StrUtil.isNotBlank(bo.getUpdateBy()), Template::getUpdateBy, bo.getUpdateBy());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(TemplateAddBo bo) {
		Template add = BeanUtil.toBean(bo, Template.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(TemplateEditBo bo) {
		Template update = BeanUtil.toBean(bo, Template.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(Template entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Transactional
	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		// 删除模板关联的字段数据
		LambdaQueryWrapper<TemplateField> lqw = Wrappers.lambdaQuery();
		lqw.in(TemplateField::getTemplateId, ids);
		iTemplateFieldService.remove(lqw);
		return removeByIds(ids);
	}
}
